package com.atwork.wuhua.ui.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.atwork.wuhua.ConstantsMyself;
import com.atwork.wuhua.R;
import com.atwork.wuhua.adapter.LaunchAdapter;
import com.atwork.wuhua.base.BaseFragment;
import com.atwork.wuhua.bean.LaunchBean;
import com.atwork.wuhua.event.SubLaunchSuccessEvent;
import com.atwork.wuhua.mvp.presenter.LaunchPresenter;
import com.atwork.wuhua.mvp.view.ILaunchFragment;
import com.atwork.wuhua.ui.activity.SubDetailActivity;
import com.atwork.wuhua.ui.activity.SubLaunchActivity;
import com.atwork.wuhua.utils.LogUtils;
import com.atwork.wuhua.utils.UserUtil;
import com.chad.library.adapter.base.BaseQuickAdapter;

import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;

public class LaunchFragment extends BaseFragment implements ILaunchFragment {
    @BindView(R.id.rv_launch)
    RecyclerView rvLaunch;
    Unbinder unbinder;
    @BindView(R.id.img_left)
    ImageView imgLeft;
    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.tv_right)
    TextView tvRight;
    @BindView(R.id.swipe)
    SwipeRefreshLayout swipe;

    private LaunchPresenter presenter;
    private LaunchAdapter launchAdapter;

    private List<LaunchBean.DataBean> launchBeans;

    private int action = MOVE_UPDATA;
    private int total;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        // TODO: inflate a fragment view
        View rootView = super.onCreateView(inflater, container, savedInstanceState);
        unbinder = ButterKnife.bind(this, rootView);
        return rootView;
    }

    @Override
    protected boolean isBindEventBus() {
        return true;
    }

    @Override
    public int intiLayout() {
        return R.layout.fragment_launch;
    }

    @Override
    public void initView() {
        imgLeft.setVisibility(View.GONE);
        tvTitle.setText("约战");
        tvRight.setVisibility(View.VISIBLE);
        tvRight.setText("发起约战");

        presenter = new LaunchPresenter();
        presenter.attachView(this);
        presenter.getData(1);
        launchBeans = new ArrayList<>();

        LinearLayoutManager manager = new LinearLayoutManager(mActivity);
        manager.setOrientation(LinearLayoutManager.VERTICAL);
        rvLaunch.setLayoutManager(manager);
        launchAdapter = new LaunchAdapter(mActivity, R.layout.item_launch, launchBeans);
        rvLaunch.setAdapter(launchAdapter);

        launchAdapter.setOnItemChildClickListener(new BaseQuickAdapter.OnItemChildClickListener() {
            @Override
            public void onItemChildClick(BaseQuickAdapter adapter, View view, int position) {
                if (UserUtil.isLoginOrOpen(getActivity())) {
                    Intent intent = new Intent(mActivity, SubDetailActivity.class);
                    intent.putExtra(ConstantsMyself.INTENTID, launchBeans.get(position).getId());
                    intent.putExtra(ConstantsMyself.INTENTTYPE, SubDetailActivity.LAUNCH);
                    startActivity(intent);
                }
            }
        });

        RefreshData();
    }

    @Override
    public void setData(LaunchBean bean) {
        if (action == MOVE_UPDATA) {
            launchBeans.clear();
        }
        total = bean.getMeta().getTotal();
        launchBeans.addAll(bean.getData());
        launchAdapter.setNewData(launchBeans);

        swipe.setRefreshing(false);
    }

    @Override
    public void noData() {
        showToast("暂无约战信息");
        swipe.setRefreshing(false);
    }

    //定义处理接收的方法 本地保存选取机台信息操作
    @Subscribe(threadMode = ThreadMode.MAIN)
    public void SubLaunchSuccessEvent(SubLaunchSuccessEvent event) {
        LogUtils.e("SubLaunchSuccessEvent");
        presenter.getData(1);
    }

    private void RefreshData() {
        swipe.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                action = MOVE_UPDATA;
                presenter.getData(1);
            }
        });

        launchAdapter.setOnLoadMoreListener(new BaseQuickAdapter.RequestLoadMoreListener() {
            @Override
            public void onLoadMoreRequested() {
                if (launchBeans.size() < total) {
                    action = MOVE_LOADING;
                    presenter.getData(launchBeans.size() / 10 + 1);
                } else {
                    launchAdapter.loadMoreEnd();//数据加载完毕
                }

            }
        }, rvLaunch);
    }

    @Override
    public void initData() {

    }


    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }


    @OnClick(R.id.tv_right)
    public void onViewClicked() {
        Intent intent = new Intent(mActivity, SubLaunchActivity.class);
        startActivity(intent);
    }


}
